from bid.models import AssetInfo,OperateInfo
from rest_framework import serializers
from loguru import logger
from datetime import datetime
from op_drf.serializers import CustomModelSerializer
from op_drf.dj_utils import flow_code_manage, check_serial_num,create_by_serializer


# 查询列表序列化
class ReadAssetInfoSerializer(CustomModelSerializer):
    class Meta:
        model = AssetInfo
        # fields = ['id', 'asset_name', 'asset_code', 'use_year',"life_year"]
        fields = "__all__"


class CreateAssetInfoSerializer(CustomModelSerializer):
    """
    新建序列化
    """
    def create(self, validated_data):
        # 增加自动生产序列号的方法
        asset_code = flow_code_manage("sys_asset_code")
        # 对编码进行验证
        check_serial_num(serial_num=asset_code, model=self.Meta.model, check_field_name="asset_code")
        logger.debug(asset_code)
        validated_data["asset_code"] = asset_code
        asset_name = validated_data["asset_name"]
        # 新建时剩余年限等于使用年限
        validated_data["life_year"] = validated_data["use_year"] if validated_data["use_year"] else 0
        instance = super().create(validated_data)
        create_by_serializer(CreateOperateInfo, data={"model": "assetinfo", "field": "asset_name", "old_value": "",
                                                      "new_value": asset_name,"zhujian":instance.pk})

        return instance
        # 合并到company

    class Meta:
        model = AssetInfo
        fields = "__all__"

class UpdateAssetInfoSerializer(CustomModelSerializer):
    """
    更新序列化
    """
    def update(self, instance, validated_data):
        asset_name = validated_data.get("asset_name","")
        create_by_serializer(CreateOperateInfo, data={"model": "assetinfo", "field": "asset_name", "old_value": instance.asset_name,
                                                      "new_value": asset_name,"zhujian":instance.pk})

        # 更新时重新计算剩余年限
        logger.info(datetime.now().year)
        logger.info(datetime.now().month)
        logger.info(datetime.now().day)
        logger.info(instance.create_time.day)
        life_year = validated_data["use_year"]
        if (datetime.now().month > instance.create_time.month) or (datetime.now().month == instance.create_time.month and datetime.now().day>instance.create_time.day):
            life_year = validated_data["use_year"] - datetime.now().year + instance.create_time.year
        validated_data["life_year"] = life_year
        return super().update(instance,validated_data)

    class Meta:
        model = AssetInfo
        fields = "__all__"


class ExportAssetSerializer(ReadAssetInfoSerializer):
    """
    导出列表序列化
    """

    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
    class Meta:
        model = AssetInfo
        fields = ["asset_code", "asset_name","create_time"]


class CreateOperateInfo(CustomModelSerializer):
    model = serializers.CharField(required=True,max_length=200)
    field = serializers.CharField(required=True,max_length=100)
    zhujian = serializers.CharField(required=True,max_length=100)
    class Meta:
        model = OperateInfo
        fields = "__all__"